//
// definition of howto scene
//
var HowtoScene = {
    scene:null,
    background:null,
    foreground:null,
    forelayer:null,
    okButton:null,
    init:function() {
        this.scene = quicktigame2d.createScene();
        
        var onloadsprite = (function(self) {
            return function(e) {
                Ti.API.info("onloadsprite: " + e.name);
                if (e.name == "graphics/title/title_background.png") {
                    game.startCurrentScene();
                }
            };
        })(this);
        
        var touchstart = (function(self) {
            return function(e) {
                // fix coordinates because view touch event returns non-retina values.
                var x = e.x * WINDOW_SCALE_FACTOR_X;
                var y = e.y * WINDOW_SCALE_FACTOR_Y;
                
                if (self.okButton.contains(x, y)) {
                    self.okButton.frame = 0;
                    picoSE.play();
                }
            };
        })(this);
        
        var touchend = (function(self) {
            return function(e) {
                // fix coordinates because view touch event returns non-retina values.
                var x = e.x * WINDOW_SCALE_FACTOR_X;
                var y = e.y * WINDOW_SCALE_FACTOR_Y;
                
                if (self.okButton.contains(x, y)) {
                    self.okButton.frame = 1;
                    game.popScene();
                }
            };
        })(this);
        
        var activated = (function(self) {
            return function(e) {
                Ti.API.info("howto scene is activated");
            
                // Set background color to black
                self.scene.color(0, 0, 0);
                
                // Create sprite instance    
                if (self.background == null) {
                    self.background = quicktigame2d.createSprite({image:'graphics/title/title_background.png'});
                }
                if (self.forelayer == null) {
                    self.forelayer = quicktigame2d.createSprite({width:self.background.width, height:self.background.height});
                    self.forelayer.color(0.5, 0.5, 0.5);
                    self.forelayer.alpha = 0.78;

                }
                if (self.foreground == null) {
                    self.foreground = quicktigame2d.createSpriteSheet({image:'graphics/howto/howto.png', width:480, height:320});
                }
                if (self.okButton == null) {
                    self.okButton = quicktigame2d.createSpriteSheet({image:'graphics/howto/howto_button.png', width:159, height:52});
                }
                
    			// set background at center of the screen
                self.background.x = (game.screen.width   * 0.5) - (self.background.width   * 0.5);
                self.background.y = (game.screen.height  * 0.5) - (self.background.height  * 0.5);
                self.forelayer.x  = (game.screen.width   * 0.5) - (self.forelayer.width    * 0.5);
                self.forelayer.y  = (game.screen.height  * 0.5) - (self.forelayer.height   * 0.5);
                self.foreground.x = (game.screen.width   * 0.5) - (self.foreground.width   * 0.5);
                self.foreground.y = (game.screen.height  * 0.5) - (self.foreground.height  * 0.5);
                
                var buttonMargin = 10;
                self.okButton.x = self.background.x + self.background.width  - self.okButton.width  - buttonMargin;
                self.okButton.y = self.background.y + self.background.height - self.okButton.height - buttonMargin;
                self.okButton.frame = 1;
                
                self.scene.add(self.background);
                self.scene.add(self.forelayer);
                self.scene.add(self.foreground);
                self.scene.add(self.okButton);
                
                self.foreground.animate(0, 2, 250, -1);

                game.addEventListener('touchstart', touchstart);
                game.addEventListener('touchend',   touchend);
            };
        })(this);
        
        var deactivated = (function(self) {
            return function(e) {
                Ti.API.info("howto scene is deactivated");
            
                // remove logo from the scene and set variable null
                // that triggers removal from the memory at GC.
                self.scene.remove(self.background);
                self.background = null;
    
                self.scene.remove(self.forelayer);
                self.forelayer = null;
                
                self.scene.remove(self.foreground);
                self.foreground = null;
                
                self.scene.remove(self.okButton);
                self.okButton = null;
                
                // If you would like to remove cached texture resource from the memory,
                // use GameView.unloadTexture(texture_name) that unloads texture resource with given name.
                game.unloadTexture('graphics/howto/howto.png');
                game.unloadTexture('graphics/howto/howto_button.png');
                
                // Stop handling touch event
                game.removeEventListener('touchstart', touchstart);
                game.removeEventListener('touchend',   touchend);;
            };
        })(this);
        
        // register scene activationi event
        this.scene.addEventListener('activated',   activated);
        this.scene.addEventListener('deactivated', deactivated);
        this.scene.addEventListener('onloadsprite',  onloadsprite);
    },
};
